package com.lxch.gdwxp.wxpay.wxpayconfig;

import com.github.wxpay.sdk.WXPayConfig;
import com.github.wxpay.sdk.WXPayUtil;
import com.lxch.gdwxp.wxpay.porperties.WXPayProperties;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 微信支付配置 实现类 此类供 微信支付SDK的WXPay类使用
 * 规定需要实现WXPayConfig
 *
 * @author lxch183
 */
public class WXPayConfigImpl implements WXPayConfig {

    private byte[] certData;//支付证书
    private static WXPayConfigImpl INSTANCE;
    private WXPayProperties wxPayProperties;

    public void setWxPayProperties(WXPayProperties wxPayProperties) {
        this.wxPayProperties = wxPayProperties;
    }

    public WXPayProperties getWxPayProperties() {
        return wxPayProperties;
    }

    @Override
    public String getAppID() {
        return wxPayProperties.getAPPID();
    }

    @Override
    public String getMchID() {
        return wxPayProperties.getMCHID();
    }

    @Override
    public String getKey() {
        return wxPayProperties.getKEY();
    }

    /**
     * 获取微信 支付证书的InputStream流
     *
     * @return 微信 支付证书的InputStream流
     */
    public InputStream getCertStream() {
        ByteArrayInputStream certBis;
        certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }

    @Override
    public int getHttpConnectTimeoutMs() {
        return wxPayProperties.getCONNECTTIMEOUTMS().intValue();
    }

    @Override
    public int getHttpReadTimeoutMs() {
        return wxPayProperties.getREADTIMEOUTMS().intValue();
    }

    /**
     * 获取HttpServletRequest中的响应流的内容 转换成map
     * 主要作用是把微信支付api返回参数的xml格式的流转换成map对象
     * @param req
     * @return 响应流封装后的map对象
     * @throws Exception
     */
    public static Map<String, String> getWXPayReturn(HttpServletRequest req) throws Exception {
        try {
            Map<String, String> map = new HashMap<String, String>();
            ServletInputStream data = req.getInputStream();
            int _buffer_size = 1024;
            if (data != null) {
                ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                byte[] tempBytes = new byte[_buffer_size];
                int count = -1;
                while ((count = data.read(tempBytes, 0, _buffer_size)) != -1) {
                    outStream.write(tempBytes, 0, count);
                }
                tempBytes = null;
                outStream.flush();
                // 将流转换成字符串
                String result = new String(outStream.toByteArray(), "UTF-8");
                System.out.println(result+"这是微信支付回调带过来的参数========");
                // 将字符串解析成XML
                // Document doc = DocumentHelper.parseText(result);
                map = WXPayUtil.xmlToMap(result);
                System.out.println(map+"这是输出的map================");
            }
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    public String getPrimaryDomain() {
        return "api.mch.weixin.qq.com";
    }

    public String getAlternateDomain() {
        return "api2.mch.weixin.qq.com";
    }
}
